import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class PDSmartRefresher extends StatefulWidget {
  final Widget child;
  final Function onLoading;
  final Function onRefresh;
  final RefreshController controller;
  final bool enablePullDown;
  final bool enablePullUp;
  const PDSmartRefresher({this.child,this.controller,this.onLoading,this.onRefresh,this.enablePullDown,this.enablePullUp,Key key}) : super(key: key);


  @override
  _PDSmartRefresherState createState() => _PDSmartRefresherState();
}

class _PDSmartRefresherState extends State<PDSmartRefresher> {
  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      enablePullUp: widget.enablePullUp ?? true,
      enablePullDown: widget.enablePullDown ?? true,
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus mode) {
          Widget body;
          if (mode == LoadStatus.idle) {
            body = Text("上拉加载更多");
          } else if (mode == LoadStatus.loading) {
            body = Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              Container(
                  width: 25, height: 25, child: CircularProgressIndicator(strokeWidth: 3)),
              SizedBox(width: 5),
              Text('正在加载...')
            ]);
          } else if (mode == LoadStatus.canLoading) {
            body = Container();
          } else {
            body = Text("没有更多数据了...");
          }
          return Container(
            height: 50.0,
            child: Center(child: body),
          );
        },
      ),
      header: CustomHeader(
        builder: (BuildContext context, mode) {
          Widget body;
          if (mode == RefreshStatus.idle) {
            body = Text("下拉刷新");
          } else if (mode == RefreshStatus.refreshing) {
            body = Text('刷新中...');
          } else if (mode == RefreshStatus.completed) {
            body = Text("刷新成功");
          } else {
            body = Text("刷新中...");
          }
          return Container(
            height: 50.0,
            alignment: Alignment.center,
            child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              Container(
                  width: 25, height: 25, child: CircularProgressIndicator(strokeWidth: 3)),
              SizedBox(width: 5),
              body
            ])
          );
        },
      ),
      onLoading: widget.onLoading,
      onRefresh: widget.onRefresh,
      controller: widget.controller,
      child: widget.child,
    );
  }
}
